package com.pro.utils;

import com.alibaba.fastjson.JSON;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author ：刘弘禹
 * @description：TODO
 * @date ：2021/12/16 18:58
 * 能够获取当前网络的ip地址与地理位置
 */
public class IPAndAddressUtils {
    public static Map<String,Object> getIPAndAddressUtils(){
        //通过该网址回去数据
        String url = "http://whois.pconline.com.cn/ipJson.jsp?json=true";
        BufferedInputStream bis = null;
        String str = null;
        Map map = new LinkedHashMap();
        try {
            URL url1 = new URL(url);
            URLConnection urlConnection = url1.openConnection();
            bis = new BufferedInputStream(urlConnection.getInputStream());
            byte b[] = new byte[1024];
            int n = -1;
            while((n = bis.read(b)) != -1) {
                str = new String(b,0,n,"GB2312");//读取文本，并输出
                map = JSON.parseObject(str, Map.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
}
